package com.kurumi.util;

import java.util.Date;

import ca.uhn.hl7v2.AcknowledgmentCode;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.v25.message.ACK;
import ca.uhn.hl7v2.model.v25.segment.MSA;
import ca.uhn.hl7v2.model.v25.segment.MSH;
import ca.uhn.hl7v2.parser.Parser;
import ca.uhn.hl7v2.parser.PipeParser;

public class HL7Util {
	
	public static ACK getACK(String sendingAppCode,String receivingAppCode,
			String messageTypeCode,String triggerEventCode,
			String hapiVersion,AcknowledgmentCode acknowledgmentCode,
			String sendMessageControlID,String textMessage) throws DataTypeException{
		ACK ack = new ACK();
		//MSH消息段(Segment)
		MSH msh = ack.getMSH();	    
		msh.getFieldSeparator().setValue("|");			
        msh.getEncodingCharacters().setValue("^~\\&");
        msh.getSendingApplication().getNamespaceID().setValue(sendingAppCode);
        msh.getSendingFacility().getNamespaceID().setValue("WinningSoft");
        msh.getReceivingApplication().getNamespaceID().setValue(receivingAppCode);
        msh.getReceivingFacility().getNamespaceID().setValue("WinningSoft");
        msh.getDateTimeOfMessage().getTime().setValue(DateUtil.dateFormat("yyyyMMddHHmmssZ", new Date()));
        msh.getMessageType().getMessageCode().setValue(messageTypeCode);
        msh.getMessageType().getTriggerEvent().setValue(triggerEventCode);
        msh.getMessageControlID().setValue(StringUtil.getUUID().toUpperCase());
        msh.getProcessingID().getProcessingID().setValue("P");
        msh.getVersionID().getVersionID().setValue(hapiVersion);
        MSA msa = ack.getMSA();
        msa.getAcknowledgmentCode().setValue(acknowledgmentCode.name());
        msa.getMessageControlID().setValue(sendMessageControlID);
        if(textMessage != null){
        	msa.getTextMessage().setValue(textMessage);
        }
        
		return ack;
	}

	/*public static void main(String[] args){
		
		try{
			
			ACK ack = new ACK();
			             
	        //MSH消息段(Segment)
	         MSH mshSegment = ack.getMSH();	    
			 mshSegment.getFieldSeparator().setValue("|");			
	         mshSegment.getEncodingCharacters().setValue("^~\\&");
	         mshSegment.getSendingApplication().getNamespaceID().setValue("MrId");
	         mshSegment.getSendingFacility().getNamespaceID().setValue("WinningSoft");
	         mshSegment.getReceivingApplication().getNamespaceID().setValue("HIS");
	         mshSegment.getReceivingFacility().getNamespaceID().setValue("WinningSoft");
	         mshSegment.getDateTimeOfMessage().getTime().setValue(DateUtil.dateFormat("yyyyMMddHHmmssZ", new Date()));
	        
	         mshSegment.getMessageType().getMessageCode().setValue("ADT");
	         mshSegment.getMessageType().getTriggerEvent().setValue("A01");
	         mshSegment.getMessageType().getMessageStructure().setValue("ADT A01");
	         mshSegment.getMessageControlID().setValue(StringUtil.getUUID().toUpperCase());
	         mshSegment.getProcessingID().getProcessingID().setValue("P");
	         mshSegment.getVersionID().getVersionID().setValue("2.5");
	         //MSH消息段(PID)
	         MSA msa = ack.getMSA();
	         msa.getAcknowledgmentCode().setValue(AcknowledgmentCode.AA.name());
	         msa.getMessageControlID().setValue("CA253461-1D38-40D2-98BC-36EA7F5BCDAE");
	         pid.getPatientIdentifierList(0).getAssigningAuthority().setValue("123456");
	
	       
	         //HL7在应用程序中的传输格式
	         Parser parser = new PipeParser();
	         String encodedMessage = parser.encode(ack);
	         System.out.println("ER7格式:");
	         System.out.println(encodedMessage);
	         
	 
	 
	         //XML格式
	         parser = new DefaultXMLParser();
	         encodedMessage = parser.encode(adt);
	         System.out.println("XML格式:");
	         System.out.println(encodedMessage);
		    } 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		      
		   

	}*/
}
